
<div id="section-title">
	<div class="wrapper">
		<div class="grid-25">
		<?php echo $html->link(__('Volver', true), array('controller'=>'products', 'action'=>'dashboard_index'), array('class'=>'btn-back right')); ?>
		<?php if (! empty($this->data['Product']['id'])): ?>
			<h1 class="title">
				<?php printf(__('Editar %s', true), $this->data['ProductDescription']['name']); ?>
			</h1>
		<?php else : ?>
			<h1 class="title">
				<?php __('Agregar nuevo producto'); ?>
			</h1>
		<?php endif; ?>
		</div>
	</div>
</div>

<div id="body">
	<div class="wrapper">
		<div class="content product-form">
			<?php echo $form->create('Product', array('url' => '/'.$this->params['url']['url'], 'type' => 'file', 'id'=>'ProductForm', 'class'=>'vrt')); ?>
			<?php echo $form->input('Product.id', array('type'=>'hidden')); ?>
			<?php echo $form->input('ProductDescription.id', array('type'=>'hidden')); ?>
		    <div class="grid-18">
		        <h4>
		            <?php __('Detalle'); ?>
		        </h4>
		
		        <div class="grid-13 alpha">
		            <?php echo $form->input('ProductDescription.name', array('type' => 'text', 'label' => __('Nombre o Modelo', true), 'class' => 'input-full', 'tabindex' => 1)); ?>
		            <?php //echo $form->input('Product.model', array('type' => 'text', 'label' => array('text' => __('Modelo', true)))); ?>
		            <?php //echo $form->input('Product.code', array('type' => 'text', 'label' => array('text' => __('Código', true)))); ?>
		        </div>
		        <div class="grid-5 omega">
		            &nbsp;
		        </div>
				
		        <div class="clear">
		        </div>
				
		        <div class="grid-13 alpha">
		            <?php echo $form->input('ProductDescription.description', array('type' => 'textarea', 'label' => __('Descripción', true), 'tabindex' => 2, 'style' => 'width:478px')); ?>
		            <?php //echo $form->input('ProductDescription.url', array('type' => 'text', 'label' => array('text' => __('URL', true)), 'after' => '<span class="help">' . __('Indica la dirección web del detalle del producto.', true) . '</span>')); ?>
		        </div>
				
		        <div class="grid-5 omega">
		            <?php echo $form->input('Product.price', array('type' => 'text', 'label' => __('Precio', true), 'class' => 'input-full', 'default' => 0, 'tabindex' => 3)); ?>
		            <?php echo $form->input('Product.currency_id', array('type' => 'select', 'options' => $currencies, 'label' => __('Moneda', true), 'empty' => array(0 => __('Seleccionar', true)), 'default' => 1, 'tabindex' => 4)); ?>
		            <?php echo $form->input('Product.disabled', array('type' => 'checkbox', 'between' => ' ', 'label' => __('Deshabilitado', true))); ?>
		        </div>
				
		        <div class="clear">&nbsp;</div>
				
		        <!--
		        <div class="offer">
		        <p><?php //__('Agrega alguna oferta a este producto.'); ?></p>
		        <?php //$html->link(__('Promoción lleva', true), array('')); ?>
		        <?php //$html->link(__('Descuento en %', true), array('controller' => 'products', 'action' => 'discount')); ?>
		        </div>
		        -->
		    </div>
		    <div class="grid-7">
		        <h4>
		            <?php //echo $html->link(__('(Agregar)', true), array('shop'=>Configure::read('Shop.slug'), 'controller'=>'taxonomies', 'action'=>'dashboard_add'), array('class'=>'right', 'id'=>'btnAddCategory')); ?>
		            <?php __('Categorías'); ?>
		        </h4>
		        <?php echo $dashboard->taxonomyProductFormCategories($categories); ?>
		        <?php echo $html->link(__('Administrar categorías', true), array('shop' => Configure::read('Shop.slug'), 'controller'=>'taxonomies', 'action'=>'dashboard_index'), array('class' => 'link-add', 'id' => 'BtnAdminCategory')); ?>
		    </div>
			
		    <div class="grid-18">
		        <h4>
		            <?php __('Imágenes'); ?>
		        </h4>
			</div>
			
			<div class="grid-18" id="ProductImages">
			<?php if (!empty($this->data['ProductImage']) || $session->check('Product.Images')): ?>
				<ul>
				<?php if (! empty($this->data['ProductImage'])): ?>
					<?php foreach ($this->data['ProductImage'] AS $image): ?>
					<li style="background-image:url(<?php echo $image['ProductImage'][0]['filename_path']; ?>);" <?php echo(($default_image = $this->data['Product']['preview'] == $image['filename']) ? ' class="default"' : ''); ?>>
						<?php echo $html->link(__('Eliminar', true), array('controller' => 'products_images', 'action' => 'delete', $image['id'], Security::hash($image['id'])), array('class'=>'ico delete')); ?>
					</li>
				<?php endforeach; ?>
				<?php endif; ?>
					<?php if ($session->check('Product.Images')): ?>
					<?php foreach ($session->read('Product.Images') AS $image): ?>
					<li style="background-image:url(<?php echo $image['url']?>);">
						<?php echo $html->link(__('Eliminar', true), array('controller' => 'products_images', 'action' => 'dashboard_delete', $image['id'], Security::hash($image['id']), 'sess'), array('class'=>'ico delete')); ?>
					</li>
				<?php endforeach; ?>
				<?php endif; ?>
				</ul>
			<?php else : ?>
		        <div class="message info">
		            <p><?php __('Haz clic en "Seleccionar foto" y selecciona la imagen del producto, recuerda que tienes un limite de 6 imagenes por producto.'); ?></p>
				</div>
			<?php endif; ?>
			</div>
			
			<div class="grid-2">
				<?php echo $html->link(__('+ Seleccionar foto', true), 'javascript:;', array('class' => 'btn btn-green', 'id' => 'BtnUploadImage')); ?>
			</div>
			
			<div class="clear">&nbsp;</div>
		
			<div class="grid-9">
				<?php echo $form->submit('Guardar', array('class'=>'btn', 'tabindex'=>5, 'after' => sprintf(__(' ó %s', true), $html->link(__('Volver atrás', true), array('shop'=>Configure::read('Shop.slug'), 'controller'=>'products', 'action'=>'dashboard_index'))))); ?>
			</div>
			
			<?php echo $form->end(); ?>
		</div>
	</div>
</div>
		

<script type="text/javascript" charset="utf-8">
	//<![CDATA[
		var uploadMax = 6;
		var uploadFile = 0;

		var messageEmpty = '<div class="message info"><p><?php __('Haz clic en "Seleccionar foto" y selecciona la imagen del producto, recuerda que tienes un limite de 6 imagenes por producto.'); ?></p></div>';
		
		(function($){
			App = new Object({
				initialize: function() {
					var self = this;
					//$('div#Product').find('button.next, button.prev, button#BtnPublish').attr('disabled', true);

					// Categories
					self.categories();
					// Images
					self.images();
					// Publicar
					self.publish();
				},
				categories: function(){
					$('#BtnAdminCategory, ul#Categories li a').live('click', function(event){
						event.preventDefault();
						
						var url = '<?php echo $html->url(array('layout' => 'iframe', 'controller' => 'taxonomies', 'action' => 'dashboard_add')); ?>';
						
						if ($(this).prev().prev().is('input[type=checkbox]')) {
							url = $(this).attr('href');
							url = url.toString().split("/");
							url = '<?php echo $html->url(array('layout' => 'iframe', 'controller' => 'taxonomies', 'action' => 'dashboard_edit')); ?>/' + url[5] + '/' + url[6];
						}
						
						$.fancybox({
							href: url,
							type: 'iframe',
							width: 792,
							height: 400,
							onStart: function() {
								//return window.confirm('Continue?');
							},
							onCancel: function() {
								//alert('Canceled!');
							},
							onComplete:	function() {
					            //alert('Completed!');
							},
							onCleanup: function() {
					            //return window.confirm('Close?');
							},
							onClosed: function() {
					            //alert('Closed!');
							}
						});
					});
				},
				images: function(){
					var uploadImages = $('#ProductImages ul li');

					var uploadScript = '<?php echo $html->url(array('controller' => 'products_images', 'action' => 'dashboard_preview')); ?>';
					/*
					if (uploadFile >= uploadMax) {
						$('#BtnUploadImage').addClass('disabled').attr('disabled', true).click(function(event){
							//event.preventDefault();
							return false;
						});
					}
					*/
					uploadImages
					.live('mouseover', function(){
						$(this).find('a').show();
					});
					
					uploadImages
					.live('mouseout', function(){
						$(this).find('a').hide();
					});
					
					uploadImages
					.find('a.delete')
					.live('click', function(){
						var self = $(this);
		
						var confirm_q = window.confirm('<?php __('¿Realmente deseas eliminar esta imagen?'); ?>');
						if (confirm_q) {
							$.post(
								self.attr('href'),
								{},
								function(data,textStatus,XMLHttpRequest){
									--uploadFile;
									self.parent().remove();

									if (uploadFile == 0) {
										$('#ProductImages').html(messageEmpty);
									}
								},
								'json'
							);
			
							if (uploadFile <= uploadMax) {
								$('#BtnUploadImage').removeClass('disabled').attr('disabled', false).click(function(event){
									return true;
								});
							}
						}
						return false;
					});
					
					var uploader = new qq.FileUploaderBasic({
						button: $('#BtnUploadImage')[0],
						action: uploadScript,
						allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
						params: {
							token: '<?php echo md5(time()); ?>'
						},
						onSubmit: function(id, fileName){
							
						},
						onProgress: function(id, fileName, loaded, total){
							
						},
						onComplete: function(id, fileName, responseJSON){
							image = responseJSON;

							var ico_delete = $('<a>').attr({'style':'display:none;','class':'ico delete', 'href':'<?php echo $html->url(array('controller' => 'products_images', 'action' => 'dashboard_delete'))?>/'+image.id+'/'+image.hash+'/sess'}).text('<?php __('Eliminar') ?>');
							var li = $('<li>').attr({style:'background-image:url('+image.url+')'}).append(ico_delete);
							if ($('#ProductImages div.message').lenght != '') {
								$('#ProductImages div.message').fadeOut();
								$('<ul>').append(li).appendTo('#ProductImages');
							}
							else {
								$('#ProductImages ul').append(li);
							}
							
							uploadFile++;
							/*
							if (uploadFile >= uploadMax) {
								$('#BtnUploadImage').addClass('disabled').attr('disabled', true).click(function(event){
									//event.preventDefault();
									return false;
								});
							}
							*/
						},
						onCancel: function(id, fileName){},
						showMessage: function(message){ /*alert(message);*/ }
					});
				},
				publish: function(){
					$('#BtnPublish').click(function(){
						$('#ProductForm').submit();
						return false;
					});
				}
			})
		})(jQuery);
		
		App.initialize();
		
		//render_editor('ProductDescriptionDescription', 'rich', false, false);
		/**
		window.onbeforeunload = function(evt) {
			var message = '<?php __('Si cierras perderas los datos ingresados de tu producto.'); ?>';
			if (typeof evt == 'undefined') {
				evt = window.event;
			}
			if (evt) {
				evt.returnValue = message;
			}
			return true;
		};*/
	//]]>
</script>
<?php //$html->css(array('/static/css/fancybox', '/static/css/uploadify'), false, array(), false); ?>
<?php //$javascript->link(array('/static/js/plugin/jquery.fancybox.1.3.1.pack','/static/js/swfobject.2','/static/js/plugin/jquery.uploadify.v2.1.4.min','/static/js/tiny_mce/tiny_mce', '/static/js/editor'), false); ?>
<?php $html->css(array($cssheet['fancybox']), false, array('inline' => false)); ?>
<?php $javascript->link(array($jscript['fancybox'], $jscript['fileuploader'], $jscript['tinymce']), false); ?>
